package com.zjx.Service.impl;

import com.zjx.Service.StudentService;
import com.zjx.Service.StudentService2;
import com.zjx.entity.Student;
import com.zjx.mapper.StudentRowMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;

import javax.sql.DataSource;
import java.sql.Types;

/**
 * @author zhaojiaxu
 * @date 2021/08/25 10:18
 **/
public class StudentServiceImpl implements StudentService {

	private JdbcTemplate jdbcTemplate;

	public void setDateSource(DataSource dateSource) {
		this.jdbcTemplate = new JdbcTemplate(dateSource);
	}

	private StudentService2 studentServiceImpl2;
	public void setStudentServiceImpl2(StudentService2 studentServiceImpl2) {
		this.studentServiceImpl2=studentServiceImpl2;
	}


	@Override
	public void save(Student student) throws Exception {
		try {
			 System.out.println("A先插入student");
			jdbcTemplate.update("insert into student(name,age,sex) values (?,?,?)",
					new Object[]{student.getName(), student.getAge(), student.getSex()}, new int[]{
							Types.VARCHAR, Types.INTEGER, Types.VARCHAR});
			//事务测试，加上这句代码则数据不会保存到数据库中
			//throw new RuntimeException("测试失败");

			//事务测试，如果抛出Exception会不会回滚呢 结果是不会产生回滚
			//throw new Exception("测试失败");
//			Student student1 = new Student();
//			student1.setId(7);
//			studentServiceImpl2.delete(student1);
		} catch (RuntimeException e) {
			e.printStackTrace();
		}

	}
}

